package fr.upmc.cie.data.dbaccess;

import java.util.ArrayList;

import fr.upmc.cie.data.TicketItem;
import fr.upmc.cie.data.exception.TicketNotFoundException;
import fr.upmc.cie.data.interfaces.ITicketDataSource;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

public class DBTicket implements ITicketDataSource{
	
	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into ticket "+
		"(title, source, date, author, link, content, isReaded, rating) values (?,?,?,?,?,?,?,?)";
	
	private SQLiteDatabase db;
	
	public DBTicket(SQLiteDatabase db) {
		this.db = db;
	}


	@Override
	public void insert(TicketItem ti){
		this.insertStmt = db.compileStatement(INSERT);
		insertStmt.bindString(1, ti.getTitle());
		insertStmt.bindString(2, ti.getSource());
		insertStmt.bindString(3, ti.getDate());
		insertStmt.bindString(4, ti.getAuthor());
		insertStmt.bindString(5, ti.getLink());
		insertStmt.bindString(6, ti.getContent());
		insertStmt.bindLong(7, ti.isReaded()?1:0);
		insertStmt.bindDouble(8, ti.getRating());
		ti.setId(insertStmt.executeInsert());
	}

	@Override
	public void delete(TicketItem ti){
		db.delete("ticket", "id_ticket=?", new String[]{String.valueOf(ti.getId())});
	}

	@Override
	public void update(TicketItem ti){
		db.update("ticket", ti.getContentValues(), "id_ticket=?", new String[]{String.valueOf(ti.getId())});
	}

	@Override
	public ArrayList<TicketItem> getAllTicket() {
		Cursor cursor = db.query("ticket", null, null, null, null, null, null);
		ArrayList<TicketItem> tl = new ArrayList<TicketItem>();
		if (cursor.moveToFirst()){
			do{
				TicketItem ti = new TicketItem();
				ti.setId(cursor.getLong(0));
				ti.setTitle(cursor.getString(1));
				ti.setSource(cursor.getString(2));
				ti.setDate(cursor.getString(3));
				ti.setAuthor(cursor.getString(4));
				ti.setLink(cursor.getString(5));
				ti.setContent(cursor.getString(6));
				ti.setReaded(cursor.getInt(7)==0?false:true);
				ti.setRating(cursor.getInt(8));
				tl.add(ti);
			}while(cursor.moveToNext());
			if (cursor != null && !cursor.isClosed()) {cursor.close();}
		}
		return tl;
	}

	@Override
	public TicketItem getTicketById(Long id) throws TicketNotFoundException {
		Cursor cursor = db.query("ticket", null, "id_ticket=?", new String[]{String.valueOf(id)}, null, null, null);
		if (cursor.moveToFirst()){ 
			TicketItem ti = new TicketItem();
			ti.setId(cursor.getLong(0));
			ti.setTitle(cursor.getString(1));
			ti.setSource(cursor.getString(2));
			ti.setDate(cursor.getString(3));
			ti.setAuthor(cursor.getString(4));
			ti.setLink(cursor.getString(5));
			ti.setContent(cursor.getString(6));
			ti.setReaded(cursor.getInt(7)==0?false:true);
			ti.setRating(cursor.getInt(8));
			if (cursor != null && !cursor.isClosed()) {cursor.close();}
			return ti;
		}else{
			throw new TicketNotFoundException();
		}
		
	}

	@Override
	public Long isTicketArchived(TicketItem ti) {
		Cursor cursor = db.query("ticket", null, "title=? AND date=?", 
				new String[]{ti.getTitle(), ti.getDate()}, null, null, null);
		if (cursor.moveToFirst()){
			Long id = cursor.getLong(0);
			if (cursor != null && !cursor.isClosed()) {cursor.close();}
			return id;
		}else{
			if (cursor != null && !cursor.isClosed()) {cursor.close();}
			return (long) 0;
		}
	}

}
